const App = getApp()
import WxResource from '../../../../helpers/WxResource'

Page({
	data: {
		show: !0,
		form: {
			linkman: '',
			phone: '',
			address: '',
			areaId: '',
		},

	},
	onLoad(option) {
		this.WxValidate = App.WxValidate({
			linkman: {
				required: true,
				minlength: 2,
				maxlength: 10,
			},
			phone: {
				required: true,
			},
			address: {
				required: true,
				minlength: 2,
				maxlength: 60,
			},
			area_id: {
				required: true,
			},
		}, {
				linkman: {
					required: '请输入收货人姓名',
				},
				phone: {
					required: '请输入收货人电话',
				},
				address: {
					required: '请输入收货人地址',
				},
				area_id: {
					required: '请选择收货人区域',
				},
			})

		this.address = App.HttpResource('/address/:id', { id: '@id' })
		this.setData({
			isShow: false, // 显示区域选择框
			id: option.id
		});


		this.area = App.HttpResource('/area/:id', { id: '@id' })
		this.area.queryAsync().then(province => {
			this.setData({
				provinces: province,
			});
			return this.area.getAsync({ id: province[0].id })
		}).then(city => {
			this.setData({
				citys: city,
			});
			return this.area.getAsync({ id: city[0].id })
		}).then(county => {
			this.setData({
				area: [0, 0, 0],
				countys: county,
			});
		});
	},
	submitForm(e) {
		const params = e.detail.value
		const id = this.data.id

		if (!this.WxValidate.checkForm(e)) {
			const error = this.WxValidate.errorList[0]
			App.WxService.showModal({
				title: '友情提示',
				content: `${error.msg}`,
				showCancel: !1,
			})
			return false
		}

		// App.HttpService.postAddress(params)
		this.address.saveAsync({ id: id }, params)
			.then(data => {

				this.showToast('修改成功')
			})
	},
	showToast(message) {
		App.WxService.showToast({
			title: message,
			icon: 'success',
			duration: 1500,
		})
			.then(() => App.WxService.navigateBack())
	},
	onShow() {
		this.renderForm(this.data.id)
	},
	renderForm(id) {
		// App.HttpService.getAddressDetail(id)
		this.address.getAsync({ id: id })
			.then(data => {
				const params = {
					linkman: data.linkman,
					phone: data.phone,
					address: data.address,
					area: data.areaName,
					area_id: data.areaId,
				}

				this.setData({
					form: params,
				})
			})
	},

	bindChange: function (e) {
		var val = e.detail.value
		var area = this.data.area;
		var _data = this.data;

		if (val[0] != area[0]) {
			this.area.getAsync({ id: _data.provinces[val[0]].id }).then(city => {
				const firstCity = city[0];
				this.setData({
					citys: city
				});
				return this.area.getAsync({ id: firstCity.id })
			}).then(county => {
				this.setData({
					area: [val[0], 0, 0],
					countys: county,
					'form.area': this.data.provinces[val[0]].name + ' - ' + this.data.citys[0].name + ' - ' + county[0].name,
					'form.area_id': county[0].id,
				});
			});
			return;
		} else if (val[1] != area[1]) {
			this.area.getAsync({ id: _data.citys[val[1]].id }).then(county => {
				this.setData({
					area: [val[0], val[1], 0],
					countys: county,
					'form.area': this.data.provinces[val[0]].name + ' - ' + this.data.citys[val[1]].name + ' - ' + county[0].name,
					'form.area_id': county[0].id,
				});
			});
			return;
		} else if (val[2] != area[2]) {
			this.setData({
				'form.area': this.data.provinces[val[0]].name + ' - ' + this.data.citys[val[1]].name + ' - ' + this.data.countys[val[2]].name,
				'form.area_id': this.data.countys[val[2]].id,
			});
			return;
		}

	},
	delete() {
		// App.HttpService.deleteAddress(this.data.id)
		this.address.deleteAsync({ id: this.data.id })
			.then(data => {
				this.showToast('删除成功')
			})
	},
	open: function () {
		this.setData({
			condition: !this.data.condition
		})
	},
})
